#include "PlayerGameData.h"
#include "MonsterData.h"
#include <QRandomGenerator>

PlayerGameData::PlayerGameData() 
    : level(1), health(80), maxHealth(80), attackPower(15), defense(5), 
      critChance(10), critMultiplier(2.0), hitChance(85), dodgeChance(8),
      totalKills(0), gameState(GameState::WAITING), isInAutoCombat(false) 
{
}

QString PlayerGameData::levelUp() 
{
    level++;
    totalKills++;
    
    QString rewardMessage = "🎯 过关奖励: ";
    
    // 检查是否是Boss关
    if (level == 10 || level == 20 || level == 30) {
        return bossReward(rewardMessage, level);
    }
    
    // 根据关卡数决定奖励机制
    if (level <= 9) {
        // 第一阶段：新手期 (1-9关) - 很难死，奖励慷慨
        return novicePhaseReward(rewardMessage);
    }
    else if (level <= 19) {
        // 第二阶段：挑战期 (11-19关) - 真正的挑战开始
        return challengePhaseReward(rewardMessage);
    }
    else if (level <= 29) {
        // 第三阶段：终极期 (21-29关) - 最高难度，为最终Boss做准备
        return ultimatePhaseReward(rewardMessage);
    }
    
    return rewardMessage + "游戏已完成！";
}

QString PlayerGameData::bossReward(QString baseMessage, quint32 bossLevel)
{
    if (bossLevel == 10) {
        // 第一阶段Boss - 慷慨奖励，为挑战期做准备
        baseMessage += "🏆 第一阶段Boss击败！获得丰厚奖励：\n";
            
        // 固定大幅提升
        quint32 attackBonus = 5 + QRandomGenerator::global()->bounded(4); // 5-8点
        quint32 healthBonus = 25 + QRandomGenerator::global()->bounded(16); // 25-40点
        quint32 defenseBonus = 2 + QRandomGenerator::global()->bounded(3); // 2-4点
        quint32 hitBonus = 4 + QRandomGenerator::global()->bounded(4); // 4-7%
            
        attackPower += attackBonus;
        maxHealth += healthBonus;
        defense += defenseBonus;
        hitChance = qMin(95u, hitChance + hitBonus);
        health = maxHealth; // 完全回复
            
        baseMessage += QString("   ⚔️ 攻击力大幅提升 +%1\n").arg(attackBonus);
        baseMessage += QString("   💪 最大血量大幅提升 +%1\n").arg(healthBonus);
        baseMessage += QString("   🛡️ 防御力提升 +%1\n").arg(defenseBonus);
        baseMessage += QString("   🎯 命中率提升 +%1%%\n").arg(hitBonus);
        baseMessage += QString("   ❤️ 完全回复所有血量\n");
            
        // 额外随机奖励（1-2个）
        quint32 extraRewards = 1 + QRandomGenerator::global()->bounded(2);
        for (quint32 i = 0; i < extraRewards; i++) {
            quint32 bonusType = QRandomGenerator::global()->bounded(5);
            if (bonusType == 0) {
                quint32 extraAttack = 2 + QRandomGenerator::global()->bounded(3);
                attackPower += extraAttack;
                baseMessage += QString("   ✨ 额外攻击力 +%1\n").arg(extraAttack);
            } else if (bonusType == 1) {
                quint32 extraHealth = 15 + QRandomGenerator::global()->bounded(11);
                maxHealth += extraHealth;
                health = maxHealth;
                baseMessage += QString("   🛡️ 额外血量上限 +%1\n").arg(extraHealth);
            } else if (bonusType == 2) {
                quint32 critBonus = 3 + QRandomGenerator::global()->bounded(3); // 3-5%
                critChance += critBonus;
                baseMessage += QString("   ⚡ 暴击率提升 +%1%%\n").arg(critBonus);
            } else if (bonusType == 3) {
                quint32 extraDefense = 1 + QRandomGenerator::global()->bounded(2);
                defense += extraDefense;
                baseMessage += QString("   🛡️ 额外防御力 +%1\n").arg(extraDefense);
            } else {
                quint32 dodgeBonus = 2 + QRandomGenerator::global()->bounded(3); // 2-4%
                dodgeChance += dodgeBonus;
                baseMessage += QString("   💨 闪避率觉醒 +%1%%\n").arg(dodgeBonus);
            }
        }
            
        return baseMessage + "🎉 准备迎接挑战期的考验！";
    }
    else if (bossLevel == 20) {
        // 第二阶段Boss - 平衡奖励，为终极期做准备
        baseMessage += "🏆 第二阶段Boss击败！获得精良装备：\n";
            
        quint32 attackBonus = 4 + QRandomGenerator::global()->bounded(3); // 4-6点
        quint32 healthBonus = 20 + QRandomGenerator::global()->bounded(11); // 20-30点
        quint32 defenseBonus = 3 + QRandomGenerator::global()->bounded(3); // 3-5点
        quint32 hitBonus = 3 + QRandomGenerator::global()->bounded(3); // 3-5%
            
        attackPower += attackBonus;
        maxHealth += healthBonus;
        defense += defenseBonus;
        hitChance = qMin(95u, hitChance + hitBonus);
        health = maxHealth;
            
        baseMessage += QString("   ⚔️ 精良武器 攻击力+%1\n").arg(attackBonus);
        baseMessage += QString("   🛡️ 精良护甲 血量上限+%1, 防御力+%2\n").arg(healthBonus).arg(defenseBonus);
        baseMessage += QString("   🎯 精准瞄具 命中率+%1%%\n").arg(hitBonus);
        baseMessage += QString("   💊 完全治愈\n");
            
        // 选择性强化（玩家风格分化）
        quint32 specialization = QRandomGenerator::global()->bounded(5);
        if (specialization == 0) {
            // 攻击特化
            quint32 extraAttack = 3 + QRandomGenerator::global()->bounded(3);
            quint32 critBonus = 4 + QRandomGenerator::global()->bounded(3); // 4-6%
            attackPower += extraAttack;
            critChance += critBonus;
            baseMessage += QString("   💥 \"锋锐\"特化 额外攻击力+%1, 暴击率+%2%%\n").arg(extraAttack).arg(critBonus);
        } else if (specialization == 1) {
            // 防御特化
            quint32 extraHealth = 20 + QRandomGenerator::global()->bounded(11);
            quint32 extraDefense = 3 + QRandomGenerator::global()->bounded(3);
            maxHealth += extraHealth;
            defense += extraDefense;
            health = maxHealth;
            baseMessage += QString("   🔰 \"坚韧\"特化 额外血量+%1, 防御力+%2\n").arg(extraHealth).arg(extraDefense);
        } else if (specialization == 2) {
            // 暴击特化
            quint32 critBonus = 6 + QRandomGenerator::global()->bounded(4); // 6-9%
            critMultiplier += 0.2 + (QRandomGenerator::global()->generateDouble() * 0.3); // +0.2-0.5
            critChance += critBonus;
            baseMessage += QString("   ⚡ \"致命\"特化 暴击率+%1%%, 暴击倍数+%2\n").arg(critBonus).arg(QString::number(critMultiplier - 2.0, 'f', 1));
        } else if (specialization == 3) {
            // 敏捷特化
            quint32 dodgeBonus = 4 + QRandomGenerator::global()->bounded(4); // 4-7%
            quint32 extraHit = 2 + QRandomGenerator::global()->bounded(3); // 2-4%
            dodgeChance += dodgeBonus;
            hitChance = qMin(95u, hitChance + extraHit);
            baseMessage += QString("   💨 \"敏捷\"特化 闪避率+%1%%, 命中率+%2%%\n").arg(dodgeBonus).arg(extraHit);
        } else {
            // 平衡特化
            quint32 balanceAttack = 2 + QRandomGenerator::global()->bounded(2);
            quint32 balanceHealth = 12 + QRandomGenerator::global()->bounded(9);
            quint32 balanceDefense = 2;
            quint32 balanceDodge = 2 + QRandomGenerator::global()->bounded(2);
            attackPower += balanceAttack;
            maxHealth += balanceHealth;
            defense += balanceDefense;
            dodgeChance += balanceDodge;
            health = maxHealth;
            baseMessage += QString("   ⚖️ \"平衡\"特化 攻击+%1, 血量+%2, 防御+%3, 闪避+%4%%\n").arg(balanceAttack).arg(balanceHealth).arg(balanceDefense).arg(balanceDodge);
        }
            
        return baseMessage + "⚡ 准备面对终极试炼！";
    }
    else if (bossLevel == 30) {
        // 最终Boss胜利 - 游戏完成
        baseMessage += "👑 最终Boss击败！游戏胜利！\n";
        baseMessage += "🎊 恭喜您完成了全部30关的挑战！\n";
        baseMessage += "🏆 您是真正的冒险大师！\n";
        baseMessage += "⏰ 游戏将在10秒后重新开始...";
            
        return baseMessage;
    }
        
    return baseMessage;
}

QString PlayerGameData::novicePhaseReward(QString baseMessage)
{
    quint32 rewardRoll = QRandomGenerator::global()->bounded(100);
        
    if (rewardRoll < 5) {
        // 5% 概率：什么都没有
        return baseMessage + "❌ 这次没有发现任何东西...";
    }
    else if (rewardRoll < 22) {
        // 17% 概率：大量回复血量
        quint32 healAmount = (maxHealth * 3) / 4 + QRandomGenerator::global()->bounded(maxHealth / 4);
        health = qMin(health + healAmount, maxHealth);
        return baseMessage + QString("❤️ 发现大型治疗药水，回复了 %1 点血量").arg(healAmount);
    }
    else if (rewardRoll < 38) {
        // 16% 概率：增加攻击力
        quint32 attackBonus = 2 + QRandomGenerator::global()->bounded(3); // 2-4点
        attackPower += attackBonus;
        return baseMessage + QString("⚔️ 获得锋利武器，攻击力增加 %1 点").arg(attackBonus);
    }
    else if (rewardRoll < 52) {
        // 14% 概率：增加最大血量
        quint32 healthBonus = 12 + QRandomGenerator::global()->bounded(14); // 12-25点
        maxHealth += healthBonus;
        return baseMessage + QString("💪 体质得到强化，最大血量增加 %1 点").arg(healthBonus);
    }
    else if (rewardRoll < 64) {
        // 12% 概率：防御力提升
        quint32 defenseBonus = 1 + QRandomGenerator::global()->bounded(2); // 1-2点
        defense += defenseBonus;
        return baseMessage + QString("🛡️ 获得护身符，防御力增加 %1 点").arg(defenseBonus);
    }
    else if (rewardRoll < 74) {
        // 10% 概率：暴击提升
        quint32 critBonus = 2 + QRandomGenerator::global()->bounded(3); // 2-4%
        critChance += critBonus;
        return baseMessage + QString("⚡ 发现幸运石，暴击率增加 %1%%").arg(critBonus);
    }
    else if (rewardRoll < 82) {
        // 8% 概率：命中率提升
        quint32 hitBonus = 2 + QRandomGenerator::global()->bounded(4); // 2-5%
        hitChance = qMin(95u, hitChance + hitBonus);
        return baseMessage + QString("🎯 获得精准镜片，命中率增加 %1%%").arg(hitBonus);
    }
    else if (rewardRoll < 88) {
        // 6% 概率：闪避率提升
        quint32 dodgeBonus = 1 + QRandomGenerator::global()->bounded(3); // 1-3%
        dodgeChance += dodgeBonus;
        return baseMessage + QString("💨 学会敏捷步法，闪避率增加 %1%%").arg(dodgeBonus);
    }
    else if (rewardRoll < 94) {
        // 6% 概率：双重发现
        quint32 attackBonus = 1 + QRandomGenerator::global()->bounded(3); // 1-3点
        quint32 healAmount = QRandomGenerator::global()->bounded(maxHealth/2, maxHealth);
            
        attackPower += attackBonus;
        health = qMin(health + healAmount, maxHealth);
        return baseMessage + QString("✨ 双重发现! 攻击力+%1, 大量回复%2血量").arg(attackBonus).arg(healAmount);
    }
    else {
        // 6% 概率：新手期大奖
        quint32 attackBonus = 2 + QRandomGenerator::global()->bounded(3); // 2-4点
        quint32 healthBonus = 15 + QRandomGenerator::global()->bounded(11); // 15-25点
        quint32 defenseBonus = 1 + QRandomGenerator::global()->bounded(2); // 1-2点
        quint32 hitBonus = 3 + QRandomGenerator::global()->bounded(3); // 3-5%
            
        attackPower += attackBonus;
        maxHealth += healthBonus;
        defense += defenseBonus;
        hitChance = qMin(95u, hitChance + hitBonus);
        health = maxHealth; // 完全回复
        return baseMessage + QString("🌟 稀有发现! 攻击力+%1, 最大血量+%2, 防御力+%3, 命中率+%4%%, 完全回复!").arg(attackBonus).arg(healthBonus).arg(defenseBonus).arg(hitBonus);
    }
}

QString PlayerGameData::challengePhaseReward(QString baseMessage)
{
    quint32 rewardRoll = QRandomGenerator::global()->bounded(100);
        
    if (rewardRoll < 25) {
        // 25% 概率：什么都没有
        return baseMessage + "💀 在战斗的废墟中什么都没找到...";
    }
    else if (rewardRoll < 38) {
        // 13% 概率：中等回复
        quint32 healAmount = QRandomGenerator::global()->bounded(maxHealth/3, (maxHealth * 2) / 3);
        health = qMin(health + healAmount, maxHealth);
        return baseMessage + QString("🩹 找到医疗包，回复了 %1 点血量").arg(healAmount);
    }
    else if (rewardRoll < 48) {
        // 10% 概率：增加攻击力
        quint32 attackBonus = 1 + QRandomGenerator::global()->bounded(3); // 1-3点
        attackPower += attackBonus;
        return baseMessage + QString("🗡️ 武器得到改良，攻击力增加 %1 点").arg(attackBonus);
    }
    else if (rewardRoll < 56) {
        // 8% 概率：增加最大血量
        quint32 healthBonus = 10 + QRandomGenerator::global()->bounded(11); // 10-20点
        maxHealth += healthBonus;
        return baseMessage + QString("🛡️ 护甲升级，最大血量增加 %1 点").arg(healthBonus);
    }
    else if (rewardRoll < 63) {
        // 7% 概率：防御力提升
        quint32 defenseBonus = 1 + QRandomGenerator::global()->bounded(3); // 1-3点
        defense += defenseBonus;
        return baseMessage + QString("🛡️ 获得强化护甲，防御力增加 %1 点").arg(defenseBonus);
    }
    else if (rewardRoll < 69) {
        // 6% 概率：暴击提升
        quint32 critBonus = 2 + QRandomGenerator::global()->bounded(4); // 2-5%
        critChance += critBonus;
        return baseMessage + QString("⚡ 找到精准镜片，暴击率增加 %1%%").arg(critBonus);
    }
    else if (rewardRoll < 74) {
        // 5% 概率：命中率提升
        quint32 hitBonus = 2 + QRandomGenerator::global()->bounded(3); // 2-4%
        hitChance = qMin(95u, hitChance + hitBonus);
        return baseMessage + QString("🎯 战斗直觉增强，命中率增加 %1%%").arg(hitBonus);
    }
    else if (rewardRoll < 78) {
        // 4% 概率：闪避率提升
        quint32 dodgeBonus = 1 + QRandomGenerator::global()->bounded(3); // 1-3%
        dodgeChance += dodgeBonus;
        return baseMessage + QString("💨 身法更加灵活，闪避率增加 %1%%").arg(dodgeBonus);
    }
    else if (rewardRoll < 85) {
        // 7% 概率：选择性奖励
        quint32 choice = QRandomGenerator::global()->bounded(4);
        if (choice == 0) {
            quint32 attackBonus = 2 + QRandomGenerator::global()->bounded(3);
            quint32 critBonus = 2 + QRandomGenerator::global()->bounded(3);
            attackPower += attackBonus;
            critChance += critBonus;
            return baseMessage + QString("⚡ 选择了\"力量之路\"，攻击力+%1, 暴击率+%2%%").arg(attackBonus).arg(critBonus);
        } else if (choice == 1) {
            quint32 healthBonus = 15 + QRandomGenerator::global()->bounded(11);
            quint32 defenseBonus = 2 + QRandomGenerator::global()->bounded(2);
            maxHealth += healthBonus;
            defense += defenseBonus;
            health = qMin(health + maxHealth/2, maxHealth);
            return baseMessage + QString("🔰 选择了\"防御之路\"，血量上限+%1, 防御力+%2, 回复一半").arg(healthBonus).arg(defenseBonus);
        } else if (choice == 2) {
            critMultiplier += 0.1 + (QRandomGenerator::global()->generateDouble() * 0.2); // +0.1-0.3
            quint32 critBonus = 3 + QRandomGenerator::global()->bounded(3);
            critChance += critBonus;
            double critMultiplierBonus = 0.1 + (QRandomGenerator::global()->generateDouble() * 0.2);
            return baseMessage + QString("💥 选择了\"致命之路\"，暴击率+%1%%, 暴击倍数+%2").arg(critBonus).arg(QString::number(critMultiplierBonus, 'f', 1));
        } else {
            quint32 dodgeBonus = 3 + QRandomGenerator::global()->bounded(3); // 3-5%
            quint32 hitBonus = 2 + QRandomGenerator::global()->bounded(3); // 2-4%
            dodgeChance += dodgeBonus;
            hitChance = qMin(95u, hitChance + hitBonus);
            return baseMessage + QString("💨 选择了\"敏捷之路\"，闪避率+%1%%, 命中率+%2%%").arg(dodgeBonus).arg(hitBonus);
        }
    }
    else {
        // 15% 概率：挑战期珍品
        quint32 rewardType = QRandomGenerator::global()->bounded(4);
        if (rewardType == 0) {
            // 攻击型珍品
            quint32 attackBonus = 2 + QRandomGenerator::global()->bounded(2); // 2-3点
            quint32 critBonus = 3 + QRandomGenerator::global()->bounded(3); // 3-5%
            attackPower += attackBonus;
            critChance += critBonus;
            health = qMin(health + (maxHealth * 2) / 3, maxHealth);
            return baseMessage + QString("💎 发现锋锐宝石! 攻击力+%1, 暴击率+%2%%, 大量回复").arg(attackBonus).arg(critBonus);
        } else if (rewardType == 1) {
            // 防御型珍品
            quint32 healthBonus = 12 + QRandomGenerator::global()->bounded(9); // 12-20点
            quint32 defenseBonus = 2 + QRandomGenerator::global()->bounded(3); // 2-4点
            maxHealth += healthBonus;
            defense += defenseBonus;
            health = qMin(health + (maxHealth * 2) / 3, maxHealth);
            return baseMessage + QString("💎 发现守护宝石! 血量上限+%1, 防御力+%2, 大量回复").arg(healthBonus).arg(defenseBonus);
        } else if (rewardType == 2) {
            // 敏捷型珍品
            quint32 dodgeBonus = 3 + QRandomGenerator::global()->bounded(4); // 3-6%
            quint32 hitBonus = 2 + QRandomGenerator::global()->bounded(4); // 2-5%
            dodgeChance += dodgeBonus;
            hitChance = qMin(95u, hitChance + hitBonus);
            health = qMin(health + (maxHealth * 2) / 3, maxHealth);
            return baseMessage + QString("💎 发现敏捷宝石! 闪避率+%1%%, 命中率+%2%%, 大量回复").arg(dodgeBonus).arg(hitBonus);
        } else {
            // 平衡型珍品
            quint32 attackBonus = 1 + QRandomGenerator::global()->bounded(2);
            quint32 healthBonus = 8 + QRandomGenerator::global()->bounded(7);
            quint32 defenseBonus = 1 + QRandomGenerator::global()->bounded(2);
            quint32 critBonus = 2 + QRandomGenerator::global()->bounded(3);
            quint32 dodgeBonus = 1 + QRandomGenerator::global()->bounded(2);
            attackPower += attackBonus;
            maxHealth += healthBonus;
            defense += defenseBonus;
            critChance += critBonus;
            dodgeChance += dodgeBonus;
            health = qMin(health + (maxHealth * 2) / 3, maxHealth);
            return baseMessage + QString("💎 发现完美宝石! 攻击+%1, 血量+%2, 防御+%3, 暴击+%4%%, 闪避+%5%%, 大量回复").arg(attackBonus).arg(healthBonus).arg(defenseBonus).arg(critBonus).arg(dodgeBonus);
        }
    }
}

QString PlayerGameData::ultimatePhaseReward(QString baseMessage)
{
    quint32 rewardRoll = QRandomGenerator::global()->bounded(100);
        
    if (rewardRoll < 40) {
        // 40% 概率：什么都没有
        return baseMessage + "💀 终极试炼的残酷...一无所获";
    }
    else if (rewardRoll < 50) {
        // 10% 概率：少量回复
        quint32 healAmount = QRandomGenerator::global()->bounded(maxHealth/4, maxHealth/2);
        health = qMin(health + healAmount, maxHealth);
        return baseMessage + QString("🌙 微弱的希望之光，回复了 %1 点血量").arg(healAmount);
    }
    else if (rewardRoll < 58) {
        // 8% 概率：攻击力提升
        quint32 attackBonus = 1 + QRandomGenerator::global()->bounded(3); // 1-3点
        attackPower += attackBonus;
        return baseMessage + QString("⚔️ 意志的力量觉醒，攻击力增加 %1 点").arg(attackBonus);
    }
    else if (rewardRoll < 65) {
        // 7% 概率：血量提升
        quint32 healthBonus = 8 + QRandomGenerator::global()->bounded(8); // 8-15点
        maxHealth += healthBonus;
        return baseMessage + QString("🩸 生命韧性提升，最大血量增加 %1 点").arg(healthBonus);
    }
    else if (rewardRoll < 71) {
        // 6% 概率：防御力提升
        quint32 defenseBonus = 1 + QRandomGenerator::global()->bounded(3); // 1-3点
        defense += defenseBonus;
        return baseMessage + QString("🛡️ 钢铁意志觉醒，防御力增加 %1 点").arg(defenseBonus);
    }
    else if (rewardRoll < 76) {
        // 5% 概率：暴击提升
        quint32 critBonus = 2 + QRandomGenerator::global()->bounded(4); // 2-5%
        critChance += critBonus;
        return baseMessage + QString("⚡ 战斗直觉锐化，暴击率增加 %1%%").arg(critBonus);
    }
    else if (rewardRoll < 80) {
        // 4% 概率：命中率提升
        quint32 hitBonus = 2 + QRandomGenerator::global()->bounded(3); // 2-4%
        hitChance = qMin(95u, hitChance + hitBonus);
        return baseMessage + QString("🎯 精神高度集中，命中率增加 %1%%").arg(hitBonus);
    }
    else if (rewardRoll < 83) {
        // 3% 概率：闪避率提升
        quint32 dodgeBonus = 2 + QRandomGenerator::global()->bounded(3); // 2-4%
        dodgeChance += dodgeBonus;
        return baseMessage + QString("💨 身如鬼魅，闪避率增加 %1%%").arg(dodgeBonus);
    }
    else if (rewardRoll < 95) {
        // 12% 概率：终极期稀有奖励
        quint32 rewardType = QRandomGenerator::global()->bounded(4);
        if (rewardType == 0) {
            // 攻击型稀有
            quint32 attackBonus = 2 + QRandomGenerator::global()->bounded(3); // 2-4点
            quint32 critBonus = 3 + QRandomGenerator::global()->bounded(4); // 3-6%
            attackPower += attackBonus;
            critChance += critBonus;
            quint32 healPercent = 40 + QRandomGenerator::global()->bounded(30); // 40-69%
            quint32 healAmount = (maxHealth * healPercent) / 100;
            health = qMin(health + healAmount, maxHealth);
            return baseMessage + QString("🔮 毁灭之力降临! 攻击力+%1, 暴击率+%2%%, 回复%3%血量").arg(attackBonus).arg(critBonus).arg(healPercent);
        } else if (rewardType == 1) {
            // 防御型稀有
            quint32 healthBonus = 10 + QRandomGenerator::global()->bounded(11); // 10-20点
            quint32 defenseBonus = 2 + QRandomGenerator::global()->bounded(3); // 2-4点
            maxHealth += healthBonus;
            defense += defenseBonus;
            quint32 healPercent = 50 + QRandomGenerator::global()->bounded(40); // 50-89%
            quint32 healAmount = (maxHealth * healPercent) / 100;
            health = qMin(health + healAmount, maxHealth);
            return baseMessage + QString("🔮 不朽之躯觉醒! 血量上限+%1, 防御力+%2, 回复%3%血量").arg(healthBonus).arg(defenseBonus).arg(healPercent);
        } else if (rewardType == 2) {
            // 暴击型稀有
            quint32 critBonus = 4 + QRandomGenerator::global()->bounded(5); // 4-8%
            critMultiplier += 0.2 + (QRandomGenerator::global()->generateDouble() * 0.3); // +0.2-0.5
            critChance += critBonus;
            quint32 healPercent = 45 + QRandomGenerator::global()->bounded(35); // 45-79%
            quint32 healAmount = (maxHealth * healPercent) / 100;
            health = qMin(health + healAmount, maxHealth);
            return baseMessage + QString("🔮 致命精准觉醒! 暴击率+%1%%, 暴击倍数+%2, 回复%3%血量").arg(critBonus).arg(QString::number(critMultiplier - 2.0, 'f', 1)).arg(healPercent);
        } else {
            // 敏捷型稀有
            quint32 dodgeBonus = 3 + QRandomGenerator::global()->bounded(4); // 3-6%
            quint32 hitBonus = 3 + QRandomGenerator::global()->bounded(4); // 3-6%
            dodgeChance += dodgeBonus;
            hitChance = qMin(95u, hitChance + hitBonus);
            quint32 healPercent = 45 + QRandomGenerator::global()->bounded(35); // 45-79%
            quint32 healAmount = (maxHealth * healPercent) / 100;
            health = qMin(health + healAmount, maxHealth);
            return baseMessage + QString("🔮 鬼神之姿觉醒! 闪避率+%1%%, 命中率+%2%%, 回复%3%血量").arg(dodgeBonus).arg(hitBonus).arg(healPercent);
        }
    }
    else {
        // 5% 概率：终极期传说奖励
        quint32 attackBonus = 3 + QRandomGenerator::global()->bounded(4); // 3-6点
        quint32 healthBonus = 15 + QRandomGenerator::global()->bounded(16); // 15-30点
        quint32 defenseBonus = 3 + QRandomGenerator::global()->bounded(4); // 3-6点
        quint32 critBonus = 5 + QRandomGenerator::global()->bounded(5); // 5-9%
        quint32 hitBonus = 3 + QRandomGenerator::global()->bounded(4); // 3-6%
        quint32 dodgeBonus = 3 + QRandomGenerator::global()->bounded(4); // 3-6%
        critMultiplier += 0.3 + (QRandomGenerator::global()->generateDouble() * 0.4); // +0.3-0.7
            
        attackPower += attackBonus;
        maxHealth += healthBonus;
        defense += defenseBonus;
        critChance += critBonus;
        hitChance = qMin(95u, hitChance + hitBonus);
        dodgeChance += dodgeBonus;
        health = maxHealth; // 完全回复
        return baseMessage + QString("👑 究极战神觉醒! 攻击+%1, 血量+%2, 防御+%3, 暴击率+%4%%, 命中率+%5%%, 闪避率+%6%%, 暴击倍数+%7, 完全回复!").arg(attackBonus).arg(healthBonus).arg(defenseBonus).arg(critBonus).arg(hitBonus).arg(dodgeBonus).arg(QString::number(critMultiplier - 2.0, 'f', 1));
    }
}

bool PlayerGameData::isAlive() const
{
    return health > 0;
}

quint32 PlayerGameData::takeDamage(quint32 incomingDamage)
{
    quint32 realDamage = (incomingDamage > defense) ? (incomingDamage - defense) : 1;
    health = (health > realDamage) ? (health - realDamage) : 0;
    return realDamage;
}

bool PlayerGameData::resetToDefault()
{
    *this = PlayerGameData();
    return true;
}

// 圆桌攻击系统实现 - 玩家攻击怪物
PlayerGameData::AttackResult PlayerGameData::performAttack(const struct MonsterData& target) const {
    AttackResult result;
    result.hit = false;
    result.critical = false;
    result.damage = 0;

    // 圆桌第一步：命中检定 (攻击者命中率 vs 防御者闪避率)
    quint32 effectiveHitChance = hitChance;
    if (target.dodgeChance > 0) {
        // 闪避率降低命中率，但命中率至少保持5%
        effectiveHitChance = qMax(5u, hitChance - target.dodgeChance);
    }

    quint32 hitRoll = QRandomGenerator::global()->bounded(100);
    if (hitRoll >= effectiveHitChance) {
        // 未命中/被闪避
        result.message = QString("💨 攻击被 %1 灵活闪避了！").arg(target.name);
        return result;
    }

    // 圆桌第二步：命中了，检查是否暴击
    result.hit = true;
    quint32 critRoll = QRandomGenerator::global()->bounded(100);
    if (critRoll < critChance) {
        // 暴击命中
        result.critical = true;
        result.damage = static_cast<quint32>(attackPower * critMultiplier);
        result.message = QString("💥⚡ 完美暴击！狠狠攻击 %1").arg(target.name);
    }
    else {
        // 普通命中
        result.damage = attackPower;
        result.message = QString("🗡️ 成功攻击 %1").arg(target.name);
    }

    return result;
} 